package TangramCore;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.MediaTracker;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.ArrayList;

public class TangramApplet extends Applet implements ObrazecListener{
	static final long serialVersionUID = 24362462L;
	
	private boolean isStandalone = true, myInitOK = false /*, mayscript*/;
  String key;
  String user,reg,acturl,htmlName,htmlId;
  URL url;
  Obrazec obrazec;

  //Skladacka tangram;
  Dilek d[], dNahled[], dVzor[];
  Obrys o, oVzor;
  ArrayList<Hladina> h;

  HladinaEdit hEdit;
  HladinaObrys hVzor, hNahled, hHelp;
  HladinaObrazek hObrazek;
  HladinaVektor hVektor;
  Database database;
  int actIndex;

  //Get a parameter value
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  //Construct the applet
  public TangramApplet() {

  }
  //Initialize the applet
  @Override
  public void init() {
    myInit("");
  }
  
  public void myInit(String s) {
    
	if(myInitOK)
      return;

    if (true) {
      setLayout(new BorderLayout());

	  database = new Database(getClass());
	  database.addSetsList("sets/skladacky.lst");
	  database.initSets(getClass());
	  database.openFile("data/tangram.tan",null); //TODO Resource
	  Skladacka skl = database.getActSet();
	  if(skl == null)
	      database.create(database.getSetsSorted()[0]);
	  d = skl.getDilky(database.getDefaultObrazecCopy().d);
	  dNahled = skl.getDilky(d);
	  o = new Obrys(dNahled);
	  actIndex = 0;
	  dVzor = database.getSize() < 1 ? database.getDefaultObrazecCopy().d : skl.getDilky(database.getObrazecData(0).d);
	  oVzor = new Obrys(dVzor);
	  h = new ArrayList<Hladina>(4);
	
	  hEdit = new HladinaEdit(d, o, Vzhled.getDefaultDilek(),Vzhled.getDefaultDilekVyber(), Vzhled.getDefaultObrys(), null, new Rectangle(0,0,-100,-100));
	  hNahled = new HladinaObrys(dNahled, o,Vzhled.getDefaultNahledDilek(),Vzhled.getDefaultNahledFilled(),null, Hladina.CENTER, false,new Rectangle(-50,0,-50,-50));
	  hVzor = new HladinaObrys(dVzor, oVzor,Vzhled.getDefaultNahledDilek(),Vzhled.getDefaultNahledFilled(),null,Hladina.CENTER, false,new Rectangle(0,0,-50,-50));
	  //hNahled = new HladinaObrys(dVzor, oVzor,Vzhled.getDefaultNahledDilek(),Vzhled.getDefaultObrys(),null,Hladina.CENTER, true,new Rectangle(-50,0,-50,-50));
	  hHelp = new HladinaObrys(dVzor, oVzor,Vzhled.getDefaultNahledDilek(),Vzhled.getDefaultHelp(),null,Hladina.DEFAULT, false,new Rectangle(0,0,-100,-100));
	  MediaTracker mt = new MediaTracker(this);
	  //hObrazek = new HladinaObrazek(getClass().getResource("pics/logo.png"),mt,Hladina.TOP | Hladina.RIGHT, false,new Rectangle(0,0,-100,-100));
	  hVektor = new HladinaVektor("pics/kresba5.emf", TangramApplet.class, Hladina.DEFAULT, true, new Rectangle(0,0,-100,-100));
	  //h.add(hObrazek);
	  //h.add(hVektor);
	  h.add(hVzor);
	  h.add(hNahled);
	  h.add(hHelp);
	  h.add(hEdit);
	  try{
	    mt.waitForAll();
	  }catch(InterruptedException e){}
	
	  add(obrazec = new Obrazec(database.getActSet(),Color.white,h,hEdit,d,dNahled,o), BorderLayout.CENTER);
	  obrazec.setSize(this.getWidth(),this.getHeight());
  //obrazec.vycentruj();
      obrazec.requestFocus();
      obrazec.addObrazecListener(this);
    }
    myInitOK = true;
  }
  //Start the applet
  public void mouseDown(MouseEvent e){
    if(e.getButton()==MouseEvent.BUTTON3 && hEdit.getVzhled() == Hladina.VZHLED_OBRYS || o.compare(oVzor))
      obrazec.prepniVzhled();
  }

  public void setActVzor(){
    Dilek.copyFrom(dVzor,database.getObrazecData(actIndex).d);
    oVzor.update(dVzor);
    obrazec.update();
  }

  public void keyDown(KeyEvent e){
    switch(e.getKeyCode()){
    case KeyEvent.VK_ENTER:
      Dilek.normalizeAll(d);
      Dilek.copyFrom(dVzor, d);
      oVzor.update(dVzor);
      obrazec.update();
      actIndex = database.addObrazecData(new Database.ObrazecData(database.getActSet(), dVzor, oVzor.od));
      //setActVzor();
      break;
    case KeyEvent.VK_A:
      hNahled.prepniVzhled();
      hVzor.prepniVzhled();
      obrazec.update();
      break;
    case KeyEvent.VK_H:
      hHelp.toggleVisibility();
      obrazec.update();
      break;
    case KeyEvent.VK_N:
      hNahled.toggleVisibility();
      hVzor.toggleVisibility();
      obrazec.update();
      break;
    case KeyEvent.VK_PAGE_DOWN:
      if(actIndex == database.getSize()-1)
        break;
      actIndex++;
      setActVzor();
      break;
    case KeyEvent.VK_PAGE_UP:
      if(actIndex == 0)
        break;
      actIndex--;
      setActVzor();
      break;
    }
  }
  public void changed(){}
  public void refresh(){
    //obrazec.refresh();
  }
  @Override
  public void start() {
      //setActObrazec(getActObrazec());
  //System.out.println("start");
  }
  //Stop the applet
  @Override
  public void stop() {
  //System.out.println("stop");
  }
  //Destroy the applet
  public void destroy() {
    //database.save(null);
  }
  //Get Applet information
  @Override
  public String getAppletInfo() {
    return "TangramBase core applet betaversion";
  }


  //Get parameter info
  @Override
  public String[][] getParameterInfo() {
    String[][] pinfo =
      {
      };
    return pinfo;
  }
  
  public String getActObrazec(){
      //Database.ObrazecData act = new Database.ObrazecData(database.getActSet(), d);
  //return act.uloz();
      return database.getActSet().ulozObrazec(d, null);
  }
  
  public void setActObrazec(String data){
    Skladacka skl = database.getActSet();
    Database.ObrazecData novy = new Database.ObrazecData(skl, skl.nactiObrazec(data));
    Dilek.copyFrom(dVzor,novy.d);
    oVzor.update(dVzor);
    obrazec.update();
  }
}
  